package com.bootdo.system.service

import com.bootdo.system.service.BitDO.Companion.BIT_TO_BYTE
import com.bootdo.system.service.BitDO.Companion.BIT_TO_GIGA_BYTE
import com.bootdo.system.service.BitDO.Companion.BIT_TO_KILO_BYTE
import org.hyperic.sigar.Cpu
import org.hyperic.sigar.CpuInfo
import org.hyperic.sigar.CpuPerc
import org.hyperic.sigar.Sigar
import java.math.BigDecimal
import java.net.InetAddress

/**
 * 计算机信息
 */
data class ComputerDO(
    //用户名
    var userName:String? = null,
    //计算机名
    var computerName:String? = null,
    //计算机域名
    var userDomain:String? = null,
    //IP地址
    var ipAddress:String? = null,
    //主机名
    var hostName:String? = null,
    //本地主机名
    var osName:String? = null,
    //JVM可以使用的总内存
    var osArch:String? = null,
    //JVM可以使用的剩余内存
    var osVersion:String? = null,
    //JVM可以使用的处理器个数
    var fileSeparator:String? = null,
    //Java的运行环境版本
    var pathSeparator:String? = null,
    //Java的运行环境供应商
    var runtimeUserName:String? = null,
    //Java供应商的URL
    var runtimeUserHome:String? = null,
    //Java的安装路径
    var runtimeUserDir:String? = null,
    //Java的虚拟机规范版本
    var totalMemory:Int? = 0,
    //Java的虚拟机规范供应商
    var usedMemory:Int? = 0,
    //Java的虚拟机规范名称
    var freeMemory:Int? = 0,
    //Java的虚拟机实现版本
    var totalSwapMemory:Int? = 0,
    //Java的虚拟机实现供应商
    var usedSwapMemory:Int? = 0,
    //Java的虚拟机实现名称
    var freeSwapMemory:Int? = 0,
    //Java运行时环境规范版本
    var cpus:Array<CpuDO>? = null
)

// 字节
data class BitDO(val size:Int = 0) {
  companion object {
    val BIT_TO_BYTE:Long = 8
    val BIT_TO_KILO_BYTE:Long = 8*1024
    val BIT_TO_MILLION_BYTE:Long = 8*1024*1024
    val BIT_TO_GIGA_BYTE:Long = 8*1024*1024*1024
  }
}
fun BitDO.toByte():String = "${BigDecimal(this.size).divide(BigDecimal.valueOf(BIT_TO_BYTE))}bit"
fun BitDO.toKiloByte():String = "${BigDecimal(this.size).divide(BigDecimal.valueOf(BIT_TO_KILO_BYTE))}bit"
fun BitDO.toMiloByte():String = "${BigDecimal(this.size).divide(BigDecimal.valueOf(BIT_TO_KILO_BYTE))}bit"
fun BitDO.toGigaByte():String = "${BigDecimal(this.size).divide(BigDecimal.valueOf(BIT_TO_GIGA_BYTE))}bit"

// cpu
data class CpuDO (
    var coreSize:Int? = 0,
    var maxMzh:BitDO? = null,
    var curMzh:BitDO? = null,
    var usage:BitDO? = null
)

// memory
data class MemoryDO (
    var totalMemery:BitDO? = null,
    var freeMemory: BitDO? = null,
    var usedMemory: BitDO? = null,
    var usage: Float? = 0f
)

// heap
data class HeapDO (
    var initialSize:BitDO? = null,
    var maxMemory:BitDO? = null,
    var usedMemory:BitDO? = null,
    var freeMemory: BitDO? = null
)

//  server
data class ServerDO (
    var serverName:String? = null,
    var osName:String? = null,
    var ipAddress: String? = null,
    var osArch: String? = null
)

data class JvmDO(
    // java运行环境版本
    var javaVersion:String? = null,
    // java虚拟机实现名称
    var jvmVersion:String? = null

)

data class PlatDO (
    var workDir:String? = null,
    var logDir:String? = null,
    var uploadDir:String? = null
)

data class DiskDO (
    var index:Int? = null
//    var
)

class PlatformService {
  val siger = Sigar()
  //获取cpu信息
  fun cpus():Array<CpuInfo> {
    return siger.cpuInfoList
  }

  // 获取cpu概述
  fun cpu():CpuDO {
    return CpuDO()
  }

  // 获取内存信息
  fun memory():MemoryDO {
    return MemoryDO()
  }

  // heap
  fun heap():HeapDO = HeapDO()

  // server
  fun server():ServerDO = ServerDO()

  // jvm
  fun jvm():JvmDO = JvmDO()

  // plat
  fun plat():PlatDO {
    return PlatDO()
  }

  // disk
  fun disks(): Array<DiskDO> {
    return arrayOf()
  }
}

fun main(args: Array<String>) {
}